package com.sl.plugin.step.selenium.mouse;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.step.ProcessCompService;
import com.sl.core.engine.step.impl.AbstractProcessComp;
import org.apache.commons.compress.utils.Lists;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.List;

/**
 */
public class SeleniumMouseComp extends AbstractProcessComp<WebDriver, WebDriver> implements ProcessCompService {


    public SeleniumMouseComp(AbstractProcessCompDescriptor abstractProcessCompDescriptor) {
        super(abstractProcessCompDescriptor);
    }


    @Override
    public WebDriver input(WebDriver driver) {

        String cssSelector = meta.getStrAttr("cssSelector");

        Boolean contextClickFlag = meta.getBoolAttr("contextClickFlag");

        Boolean clickAndHoldFlag = meta.getBoolAttr("clickAndHoldFlag");

        Boolean doubleClickFlag = meta.getBoolAttr("doubleClickFlag");

        Boolean moveToElementFlag = meta.getBoolAttr("moveToElementFlag");

        //鼠标事件
        Actions actions = new Actions(driver);
        WebElement element = driver.findElement(By.cssSelector(cssSelector));
        if (ObjectUtil.isNull(element)) {
            logBasic("没有获取到元素" + cssSelector);
            return driver;
        }

        if (BooleanUtil.isTrue(contextClickFlag)) {
            actions.contextClick(element).perform();
        }
        if (BooleanUtil.isTrue(clickAndHoldFlag)) {
            actions.clickAndHold(element).perform();
        }
        if (BooleanUtil.isTrue(doubleClickFlag)) {
            actions.doubleClick(element).perform();
        }
        if (BooleanUtil.isTrue(moveToElementFlag)) {
            actions.moveToElement(element).perform();
            //actions.moveToElement(element,x,y).perform(); //指定位置
            //将鼠标从其当前位置（或0,0） 移动鼠标 ,如果提供的坐标在视口之外（鼠标将在浏览器窗口之外结束），则会滚动视口以匹配。
            //actions.moveToElement(x,y).perform();
            //拖动元素, 在源元素的位置执行点击并保持，移动到目标元素的位置，然后释放鼠标
            //Actions dragAndDrop(WebElement source, WebElement target)
            //Actions dragAndDropBy(WebElement source, int xOffset, int yOffset) 拖动到指定位置
        }

        return driver;
    }

}
